package com.tebiecloud.order.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.common.constants.ResultEnum;
import com.tebiecloud.common.security.SecurityHelper;
import com.tebiecloud.common.security.SecurityUserDetails;
import com.tebiecloud.common.utils.BeanConvertUtils;
import com.tebiecloud.common.utils.StringUtils;
import com.tebiecloud.mpweixin.client.model.vo.PlusUserAmountVo;
import com.tebiecloud.order.client.model.entity.OrderEvaluate;
import com.tebiecloud.order.client.model.entity.OrderGoods;
import com.tebiecloud.order.client.model.entity.Orderr;
import com.tebiecloud.order.client.model.vo.EvaluateVO;
import com.tebiecloud.order.server.service.OrderEvaluateService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import com.tebiecloud.order.server.service.OrderGoodsService;
import com.tebiecloud.order.server.service.OrderrService;
import com.tebiecloud.order.server.service.feign.MpweixinServiceClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;

import java.awt.*;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 订单评价表 前端控制器
 * @author tebie
 * @date 2019-10-28
 */
@RestController
@RequestMapping("/orderevaluate")
public class OrderEvaluateController {

    @Autowired
    private OrderEvaluateService orderEvaluateService;

    @Autowired
    private OrderrService orderrService;

    @Autowired
    private OrderGoodsService orderGoodsService;

    @Autowired
    private MpweixinServiceClient mpweixinServiceClient;

    /**
     * 添加评价
     * @param orderId 订单id,
     * @param remark 备注,
     * @param satisfaction 满意度:1~5级,
     * @param intention 用心度:1~5级,
     * @param sharpness 到位程度:1~5级,
     * @param image 图片链接
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "orderId", required = false) Long orderId,
        @RequestParam(value = "remark", required = false) String remark,
        @RequestParam(value = "satisfaction", required = false) String satisfaction,
        @RequestParam(value = "intention", required = false) String intention,
        @RequestParam(value = "sharpness", required = false) String sharpness,
        @RequestParam(value = "image", required = false) String image) {
        SecurityUserDetails securityUserDetails = SecurityHelper.getUser();
        long userId = securityUserDetails.getUserId();
        //查询订单
        QueryWrapper<Orderr> orderrQueryWrapper = new QueryWrapper<>();
        orderrQueryWrapper.eq("order_id",orderId);
        orderrQueryWrapper.eq("status",4);//完成订单才可以评价
        Orderr orderr = orderrService.getOne(orderrQueryWrapper);
        if(orderr==null)return ResultBodyUtils.failedMsg("订单查询不存在");
        if(orderr.getUserId()!=null && !orderr.getUserId().equals(userId)){
            return ResultBodyUtils.failedMsg("只能评价自己的订单");
        }
        //查询订单返回乖乖币
        QueryWrapper<OrderGoods> orderGoodsQueryWrapper = new QueryWrapper<>();
        orderGoodsQueryWrapper.eq("order_id",orderId);
        List<OrderGoods> orderGoodsList = orderGoodsService.list(orderGoodsQueryWrapper);
        int totalCoin = 0;//总返乖乖币
        if(orderGoodsList!=null && orderGoodsList.size()>0){
            for(OrderGoods orderGoods:orderGoodsList){
                totalCoin = totalCoin + orderGoods.getCoin();
            }
        }
        OrderEvaluate orderEvaluate = new OrderEvaluate();
        orderEvaluate.setOrderId(orderId);
        orderEvaluate.setUserId(userId);
        orderEvaluate.setRemark(remark);
        orderEvaluate.setSatisfaction(satisfaction);
        orderEvaluate.setIntention(intention);
        orderEvaluate.setSharpness(sharpness);
        orderEvaluate.setCoin(totalCoin);
        orderEvaluate.setImage(image);
        orderEvaluateService.save(orderEvaluate);
        return ResultBodyUtils.ok(orderId);
    }

    /**
     * 根据ID查找数据
     * @param orderEvaluateId
     * @return
     */
    @PostMapping("get")
    public ResultBody<EvaluateVO> get(@RequestParam("orderEvaluateId") Long orderEvaluateId) {
        OrderEvaluate orderEvaluate = orderEvaluateService.getById(orderEvaluateId);
        EvaluateVO evaluateVO = BeanConvertUtils.convertBean(orderEvaluate,EvaluateVO.class);
        if(StringUtils.isNotEmpty(orderEvaluate.getImage())){
            String[] images = orderEvaluate.getImage().split(",");
            evaluateVO.setImages(Arrays.asList(images));
        }
        return ResultBodyUtils.ok(evaluateVO);
    }

//    /**
//     * 获取分页数据
//     * @param map
//     * @return
//     */
//    @GetMapping(value = "list")
//    public ResultBody<IPage<OrderEvaluate>> list(@RequestParam(required = false) Map map) {
//        PageParams pageParams = new PageParams(map);
//        OrderEvaluate query = pageParams.mapToObject(OrderEvaluate.class);
//        QueryWrapper<OrderEvaluate> queryWrapper = new QueryWrapper();
//        return ResultBodyUtils.ok(orderEvaluateService.page(new PageParams(map), queryWrapper));
//    }
//
//    /**
//     * 根据ID查找数据
//     * @param id
//     * @return
//     */
//    @GetMapping("get")
//    public ResultBody<OrderEvaluate> get(@RequestParam("id") Long id) {
//        OrderEvaluate entity = orderEvaluateService.getById(id);
//        return ResultBodyUtils.ok(entity);
//    }
//
//    /**
//     * 更新数据
//     * @param orderId 订单id
//     * @param userId 用户id
//     * @param remark 备注
//     * @param satisfaction 满意度:1~5级
//     * @param intention 用心度:1~5级
//     * @param sharpness 到位程度:1~5级
//     * @param coin 评论获取的乖乖币
//     * @param image 图片链接
//     * @return
//     */
//    @PostMapping("update")
//    public ResultBody update(
//            @RequestParam(value = "evaluateId", required = false) Long evaluateId,
//            @RequestParam(value = "orderId", required = false) Long orderId,
//            @RequestParam(value = "userId", required = false) Long userId,
//            @RequestParam(value = "remark", required = false) String remark,
//            @RequestParam(value = "satisfaction", required = false) String satisfaction,
//            @RequestParam(value = "intention", required = false) String intention,
//            @RequestParam(value = "sharpness", required = false) String sharpness,
//            @RequestParam(value = "coin", required = false) Integer coin,
//            @RequestParam(value = "image", required = false) String image
//    ) {
//        OrderEvaluate entity = new OrderEvaluate();
//        entity.setEvaluateId(evaluateId);
//        entity.setOrderId(orderId);
//        entity.setUserId(userId);
//        entity.setRemark(remark);
//        entity.setSatisfaction(satisfaction);
//        entity.setIntention(intention);
//        entity.setSharpness(sharpness);
//        entity.setCoin(coin);
//        entity.setImage(image);
//        orderEvaluateService.updateById(entity);
//        return ResultBodyUtils.ok();
//    }
//
//    /**
//     * 删除数据
//     * @param id 单个id
//     * @param ids 多个id用,号隔开
//     * @return
//     */
//    @PostMapping("remove")
//    public ResultBody remove(
//            @RequestParam(value = "id", required = false) Long id,
//            @RequestParam(value = "ids", required = false) String ids
//    ) {
//        if(id != null && id > 0) {
//            orderEvaluateService.removeById(id);
//        }
//        if(ids != null && ids.length() > 0) {
//            orderEvaluateService.removeByIds(Arrays.asList(ids.split(",")));
//        }
//        return ResultBodyUtils.ok();
//    }

}
